<!--  -->
<template>
  <section class="wh_container">
    <div class="wh_content_all">
      <div class="wh_top_changge" :style="{'border-bottom': isBorder ? '1px solid #eee' :  ''}">
        <li @click="PreMonth(myDate, false)">
          <a-icon type="caret-left" />
        </li>
        <li class="wh_content_li">{{ dateTop }}</li>
        <li @click="NextMonth(myDate, false)">
          <a-icon type="caret-right" />
        </li>
      </div>
      <div class="wh_content week-content">
        <div class="wh_content_item" v-for="tag in textTop">
          <div class="wh_top_tag">{{ tag }}</div>
        </div>
      </div>
      <div class="wh_content">
        <div
          class="wh_content_item"
          v-for="(item, index) in list"
          @click="clickDay(item, index)"
        >
          <div
            class="wh_item_date"
            v-bind:class="[
              { wh_isMark: item.isMark },
              { wh_other_dayhide: item.otherMonth !== 'nowMonth' },
              { wh_want_dayhide: item.dayHide },
              { wh_isToday: item.isToday },
              { wh_chose_day: item.chooseDay },
              { has_course: item.course }
            ]"
          >{{item.id}}
          </div>
        </div>
      </div>
    </div>
  </section>
</template>

<script>
import timeUtil from "./calendar";

export default {
  name: "yxp-canlendar",
  data() {
    return {
      myDate: [],
      list: [],
      historyChose: [],
      dateTop: ""
    };
  },
  props: {
    markDate: {
      type: Array,
      default: () => []
    },
    markDateMore: {
      type: Array,
      default: () => []
    },
    textTop: {
      type: Array,
      default: () => ["一", "二", "三", "四", "五", "六", "日"]
    },
    sundayStart: {
      type: Boolean,
      default: () => false
    },
    agoDayHide: {
      type: String,
      default: `0`
    },
    futureDayHide: {
      type: String,
      default: `2554387200`
    },
    isBorder: {
      type: Boolean,
      default: false
    }
  },
  created() {
    this.intStart();
    this.myDate = new Date();
  },
  mounted () {
    console.log("++++++++++++++++%%%%%%%%%%%%%%%")
    console.log(this.markDateMore)
  },
  methods: {
    intStart() {
      timeUtil.sundayStart = this.sundayStart;
    },
    clickDay(item, index) {
      if (!item.dayHide) {
        if (item.otherMonth === "nowMonth" && !item.dayHide) {
          this.getList(this.myDate, item.date);
        }
        if (item.otherMonth !== "nowMonth") {
          item.otherMonth === "preMonth"
            ? this.PreMonth(item.date)
            : this.NextMonth(item.date);
        }
      }
    },
    // ChoseMonth: function(date, isChosedDay = true) {
    //   date = timeUtil.dateFormat(date);
    //   this.myDate = new Date(date);
    //   console.log(arr)
    //   this.$emit("changeMonth", timeUtil.dateFormat(this.myDate));
    //   if (isChosedDay) {
    //     this.getList(this.myDate, date, isChosedDay);
    //   } else {
    //     this.getList(this.myDate);
    //   }
    // },
    PreMonth: function(date, isChosedDay = true) {
      date = timeUtil.dateFormat(date);
      this.myDate = timeUtil.getOtherMonth(this.myDate, "preMonth");
      this.$emit("changeMonth", timeUtil.dateFormat(this.myDate));
      if (isChosedDay) {
        this.getList(this.myDate, date, isChosedDay);
      } else {
        this.getList(this.myDate, true);
      }
    },
    NextMonth: function(date, isChosedDay = true) {
      date = timeUtil.dateFormat(date);
      this.myDate = timeUtil.getOtherMonth(this.myDate, "nextMonth");
      this.$emit("changeMonth", timeUtil.dateFormat(this.myDate));
      if (isChosedDay) {
        this.getList(this.myDate, date, isChosedDay);
      } else {
        this.getList(this.myDate, true);
      }
    },
    forMatArgs: function() {
      let markDate = this.markDate;
      let markDateMore = this.markDateMore;
      console.log(this.markDateMore)
      markDate = markDate.map(k => {
        return timeUtil.dateFormat(k);
      });
      markDateMore = markDateMore.map(k => {
        k.date = timeUtil.dateFormat(k.date);
        return k;
      });
      return [markDate, markDateMore];
    },
    getList: function(date, chooseDay, isChosedDay = true) {
      console.log(date, chooseDay, isChosedDay,'-----2222222222222')
      const [markDate, markDateMore] = this.forMatArgs();
      console.log(markDate)
      console.log(markDateMore)
      this.dateTop = `${date.getFullYear()}年${date.getMonth() + 1}月`;
      let arr = timeUtil.getMonthList(this.myDate);
      console.log(arr)
      console.log("wo de ces")
      for (let i = 0; i < arr.length; i++) {
        let markClassName = "";
        let course = "";
        let k = arr[i];
        k.chooseDay = false;
        const nowTime = k.date;
        const t = new Date(nowTime).getTime() / 1000;
        //看每一天的class
        for (const c of markDateMore) {
          if (c.date === nowTime) {
            markClassName = c.className || "";
            course = c.course;
          }
        }
        //标记选中某些天 设置class
        k.markClassName = markClassName;
        k.course = course;
        k.isMark = markDate.indexOf(nowTime) > -1;
        //无法选中某天
        k.dayHide = t < this.agoDayHide || t > this.futureDayHide;
        if (k.isToday) {
          this.$emit("isToday", nowTime);
        }
        let flag = !k.dayHide && k.otherMonth === "nowMonth";
        if (chooseDay && chooseDay === nowTime && flag) {
          this.$emit("choseDay", k);
          this.historyChose.push(nowTime);
          k.chooseDay = true;
        } else if (
          this.historyChose[this.historyChose.length - 1] === nowTime &&
          !chooseDay &&
          flag
        ) {
          k.chooseDay = true;
        }
      }
      this.list = arr
      console.log(this.list)
    }
  },
  mounted() {
    this.getList(this.myDate);
  },
  watch: {
    markDate: {
      handler(val, oldVal) {
        this.getList(this.myDate);
      },
      deep: true
    },
    markDateMore: {
      handler(val, oldVal) {
        this.getList(this.myDate);
      },
      deep: true
    },
    agoDayHide: {
      handler(val, oldVal) {
        this.getList(this.myDate);
      },
      deep: true
    },
    futureDayHide: {
      handler(val, oldVal) {
        this.getList(this.myDate);
      },
      deep: true
    },
    sundayStart: {
      handler(val, oldVal) {
        this.intStart();
        this.getList(this.myDate);
      },
      deep: true
    }
  }
};
</script>
<style lang="less" scoped>
/* @import url(); 引入公共css类 */
@import url("./index.less");
</style>
